package mysql

import (
	"baidu/appconfig"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
)

var DB *gorm.DB
var err error

func Init() {
	data := appconfig.ConfigData.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", data.User, data.Pass, data.Host, data.Port, data.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("数据库连接失败")
	} else {
		log.Println("数据库连成功")
	}
	err = DB.AutoMigrate(&User{}, &Types{}, &Text{}, &Book{}, &Likes{}, &Sell{})
	if err != nil {
		panic("数据库迁移失败")
	} else {
		log.Println("数据迁移成功")
	}
}
